import { LCrypto } from '../utils/lcrypto';
export class TokenMgr
{
    private static tokens = {};
    private static users = {};
    static createToken = function(userId,lifeTime){
        var token = TokenMgr.users[userId];
        if(token != null){
            this.delToken(token);
        }
        var time = Date.now();
        token = LCrypto.md5(userId + "!@#$%^&" + time);
        TokenMgr.tokens[token] = {
            userId: userId,
            time: time,
            lifeTime: lifeTime
        };
        TokenMgr.users[userId] = token;
        return token;
    }
    static getToken = function(userId){
        return TokenMgr.users[userId];
    };
    
    static getUserID = function(token){
        return TokenMgr.tokens[token].userId;
    };
    
    static isTokenValid = function(token){
        var info = TokenMgr.tokens[token];
        if(info == null){
            return false;
        }
        if(info.time + info.lifetime < Date.now()){
            return false;
        }
        return true;
    };
    
    static delToken = function(token){
        var info = TokenMgr.tokens[token];
        if(info != null){
            TokenMgr.tokens[token] = null;
            TokenMgr.users[info.userId] = null;
        }
    };
}